﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.Model.Entity;
using PPAP.BO;

namespace PPAP.Web.SearchReport
{
    public partial class PPARSearch : PageBase
    {
        PPARBO bo = new PPARBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bindData();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            bindData();
        }

        public void bindData()
        {
            try
            {
                DataTable dt = GetDataSet("").Tables[0];
                // dt.Columns.Add("ShowStatus");
                foreach (DataRow r in dt.Rows)
                {
                    r["ShowStatus"] = ShowStatus(r["status"].ToString(),r["FINALSTATUS"].ToString(),r["isinapp"].ToString());
                }
                AspNetPager2.RecordCount = dt.Rows.Count;
                dgList.DataSource = GetPageTable(dt, AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
                dgList.DataBind();
            }
            catch (Exception ex)
            {
                AlertMessage(ex.Message.ToString());
            }
        }

        private DataSet GetDataSet(string sort)
        {
            PPAP.Model.Search.PPARSearch ps = new Model.Search.PPARSearch();
            ps.PartNo = this.tbPartno.Text;
            ps.SupplierNo = this.tbSupplierNo.Text;
            if (this.DatePicker1.Text != "")
                ps.StartDate1 = base.ToDateTime(this.DatePicker1.Text, "error format!");
            if (this.DatePicker2.Text != "")
                ps.StartDate2 = base.ToDateTime(this.DatePicker2.Text, "error format!");
            ps.SamplingReason = this.tbSamplingReason.Text;
            ps.Version = this.tbVersion.Text;
            if (this.ddlStatus.SelectedValue != "")
                ps.Status = base.ToInt32(this.ddlStatus.SelectedValue, "error format!");
            ps.MyCase = this.chkMycase.Checked;
            ps.CreateUser = CurrentUser.Id;

            DataSet dt = bo.GetPPARHistory(ps,sort);
            return dt;
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            BindGridView();
        }

        

        //状态显示
        public string ShowStatus(string Status, string FINALSTATUS,string isinapp)
        {
            string result = "";
            result = "";

            switch (Status.ToLower())
            {
                case "0":
                    if (isinapp == "0")
                        result = "<span style='color:red'>Un-allocated</span>";
                    else
                        result = "<span style='color:blue'>Allocated</span>";
                    break;
                case "1":
                    result = "<span style='color:red'>Created</span>";
                    break;
                case "2":
                    result = "<span style='color:red'>Submitted</span>";
                    break;
                case "3":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "4":
                    result = "<span style='color:red'>Not ready for sampling</span>";
                    break;
                case "5":
                    result = "<span style='color:red'>Circulating</span>";
                    break;
                case "6":
                    if (FINALSTATUS == "0")
                        result = "<span style='color:red'>Circulating</span>";
                    else
                        result = "<span style='color:blue'>Released</span>";
                    break;
                case "7":
                    result = "<span style='color:blue'>Decided</span>";
                    break;

                default:
                    result = "<span style='color:red'></span>";
                    break;
            }
            return result;
        }

        /// <summary>
        /// Export
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = GetDataSet("");
                //ds.Tables[0].Columns.Add("ShowStatus");

                foreach (DataRow r in ds.Tables[0].Rows)
                {
                    r["ShowStatus"] = ShowStatus(r["status"].ToString(),r["FINALSTATUS"].ToString(),r["isinapp"].ToString());
                    r["d1"] = r["PPARDATE"].ToString() == "" ? r["PPARDATE"] : DateTime.Parse(r["PPARDATE"].ToString()).ToString("yyyy-MM-dd");
                    r["d2"] = r["ENDTIME"].ToString() == "" ? r["ENDTIME"] : DateTime.Parse(r["ENDTIME"].ToString()).ToString("yyyy-MM-dd");
                }
                ds.Tables[0].Columns.Remove("ID");
                ds.Tables[0].Columns.Remove("BDNTREVISIONNO");
                ds.Tables[0].Columns.Remove("BDNTREVISIONSTATUS");
                ds.Tables[0].Columns.Remove("REASONFORSAMPLING");
                ds.Tables[0].Columns.Remove("STATUS");
                ds.Tables[0].Columns.Remove("DECISIONTEXT");
                ds.Tables[0].Columns.Remove("EXPLANATIONS");
                //ds.Tables[0].Columns.Remove("FINALRESULT");
                ds.Tables[0].Columns.Remove("RESUBMISSIONDATE");
                ds.Tables[0].Columns.Remove("DSC");
                ds.Tables[0].Columns.Remove("CONTACTTEL");
                ds.Tables[0].Columns.Remove("ADDRESS");
                //ds.Tables[0].Columns.Remove("SUPPLIERCODE");
               // ds.Tables[0].Columns.Remove("PPARNO1");
                ds.Tables[0].Columns.Remove("BDNTPARTNAME");
                ds.Tables[0].Columns.Remove("SUPPLIERNAME");
                ds.Tables[0].Columns.Remove("SUPPLIERPARTNO");
                ds.Tables[0].Columns.Remove("PPARDATE");
                ds.Tables[0].Columns.Remove("ENDTIME");
                ds.Tables[0].Columns.Remove("PPARVERSION");
                ds.Tables[0].Columns.Remove("FINALSTATUS");
                ds.Tables[0].Columns.Remove("isinapp");
                ds.Tables[0].Columns["ShowStatus"].ColumnName = "Status";

                ds.Tables[0].Columns["ROWNUM"].ColumnName = "No.";
                ds.Tables[0].Columns["BDNTPARTNO"].ColumnName = "Part.No";
                ds.Tables[0].Columns["PPARNO"].ColumnName = "PPAR.No";
                ds.Tables[0].Columns["QLEVEL"].ColumnName = "Q-level";
                ds.Tables[0].Columns["SUPPLIERCODE"].ColumnName = "Supplier Code";
                //ds.Tables[0].Columns["PPARVERSION"].ColumnName = "PPAR Version";
                ds.Tables[0].Columns["d1"].ColumnName = "Start date";
                ds.Tables[0].Columns["d2"].ColumnName = "End date";
                ds.Tables[0].Columns["FINALRESULT"].ColumnName = "Result";
                ds.Tables[0].Columns["REMARKS"].ColumnName = "Remark";
                CreateExcel(ds, "PPAR");
            }
            catch (Exception ex)
            {
                AlertMessage(ex.Message.ToString());
            }
        }

        protected void dgList_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.dgList.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.dgList.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.dgList.Attributes["SortExpression"] = sortExpression;
            this.dgList.Attributes["SortDirection"] = sortDirection;


            string pic = "";
            pic = sortDirection == "ASC" ? "<img src='../Images/icon/arrowup.gif' />" : "<img src='../Images/icon/arrowdown.gif' />";

            foreach (DataGridColumn item in dgList.Columns)
            {
                if (item.SortExpression == sortExpression)
                {
                    item.HeaderText = item.FooterText + " " + pic;
                }
                else
                    item.HeaderText = item.FooterText;
            }

            this.BindGridView();

        }

        private void BindGridView()
        {
            // 获取GridView排序数据列及排序方向
            string sortExpression = this.dgList.Attributes["SortExpression"];
            string sortDirection = this.dgList.Attributes["SortDirection"];
            if (sortExpression != null)
            {
                // 调用业务数据获取方法
                DataTable dtBind = GetDataSet(string.Format("  order by lower({0})  {1} ", sortExpression, sortDirection)).Tables[0];
                foreach (DataRow r in dtBind.Rows)
                {
                    r["ShowStatus"] = ShowStatus(r["status"].ToString(), r["FINALSTATUS"].ToString(), r["isinapp"].ToString());
                }
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }
                // GridView绑定并显示数据
                this.dgList.DataSource = GetPageTable(dtBind, AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
                this.dgList.DataBind();
            }else
                btnSubmit_Click(null, null);
        }
    }
}